//
//  ForumViewController.m
//  Cars' home
//
//  Created by dapeng on 15/11/27.
//  Copyright © 2015年 dapeng. All rights reserved.
//

#import "ForumViewController.h"
#import "Theme-PrefixHeader.pch"
@interface ForumViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate, ForumCollectionViewCellDelegate, hotForumCollectionViewCellDelegate>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *buttonArray;
@property (nonatomic, retain) UIScrollView *scrollView;
@end

@implementation ForumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self collectionViews];
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [[ConfigurationTheme shareInstance] getThemeColorWithName:@"ygColor"];
    [self.view addSubview:self.collectionView];
    [_collectionView release];
    self.collectionView.backgroundColor = [UIColor redColor];
    self.buttonArray = [[NSMutableArray array] init];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 30)];
    NSArray *array = [NSArray arrayWithObjects:@"全部帖子", @"热门帖子", nil];
    for (int i = 0; i < 2; i++) {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * 120, 0, 100, 30);
        button.tag = i + 100;
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonActions:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            
        }
        [self.buttonArray addObject:button];
        [self.scrollView addSubview:button];
        [button release];
        
    }
    
    self.navigationItem.titleView = self.scrollView;
    
    
    // Do any additional setup after loading the view.
}

//改变主题的通知

- (void)changeColor {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"nightModel"];
    
    if (!(passWord == nil) && [passWord isEqualToString:@"day"]) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
        self.tabBarController.tabBar.barTintColor = [UIColor colorWithWhite:0.126 alpha:1.000];
        
    }else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"forumPic"] forBarMetrics:UIBarMetricsDefault];
        self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];

    }
    
}

- (void)collectionViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 49);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[SAPForumCollectionViewCell class] forCellWithReuseIdentifier:@"SAPFindCarCollectionViewCellIndentifier"];
    [_collectionView registerClass:[SAPHotForumCollectionViewCell class] forCellWithReuseIdentifier:@"SAPHotForumCollectionViewCellIndentifier"];
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
  
    if (indexPath.item == 0) {
    SAPForumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPFindCarCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    if (indexPath.item == 1) {
    SAPHotForumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SAPHotForumCollectionViewCellIndentifier" forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }
    return nil;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (void)passWebView:(UIWebView *)web {
    SAPForumWebViewController *webView = [[SAPForumWebViewController alloc] init];
    [webView.view addSubview:web];
    [self.navigationController pushViewController:webView animated:YES];
}
- (void)hotPassWebView:(UIWebView *)web {
    SAPForumWebViewController *webView = [[SAPForumWebViewController alloc] init];
    [webView.view addSubview:web];
    [self.navigationController pushViewController:webView animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)buttonActions:(UIButton *)sender {
    [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    for (int i = 0; i < self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        if (sender.tag != button.tag) {
            [self.buttonArray[i] setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
    
    [self.collectionView setContentOffset:CGPointMake((sender.tag - 100) * self.view.frame.size.width, 0)];
    [self.collectionView reloadData];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    int temp = self.collectionView.contentOffset.x / SCREEN_SIZE.width;
    UIButton *button = self.buttonArray[temp];
    if (self.collectionView.contentOffset.x < SCREEN_SIZE.width * 4) {
        self.scrollView.contentOffset = CGPointMake(0, 0);
    }
    else if (self.collectionView.contentOffset.x >= SCREEN_SIZE.width * 4) {
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + 60, 0);
    }
    [self buttonActions:button];
}
- (void)viewWillAppear:(BOOL)animated {
    [self changeColor];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
